define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {
		$scope.form.father_id = params.father_id;
		$scope.form.fk_class = params.id;
		$scope.form.fk_unit = 'self';
		$scope.isClassMajorShow = app.parameter.parameter_isClassMajorShow;

		//查询主校区和分校区
		$scope.findUnit = function() {
			httpService.post(app.urls.unit_selectSchoolAndAttach, {"fk_unit": $scope.form.fk_unit}, function (data) {
				if ("0000" === data.code) {
					$scope.unitList = data.data;
					$scope.findGrade();
				} else {
					alert(data.msg);
				}
			});
		}

		//查询年级
		$scope.findGrade = function() {
			httpService.post(app.urls.grade_selectCenterGrade, {"fk_unit":$scope.form.fk_unit}, function(data) {
				if("0000" === data.code){
					$scope.gradeList = data.data;
					$scope.findclassroom();
				}else{
					alert(data.msg);
				}
			});
		}


		//查询教室
		$scope.findclassroom = function() {
			httpService.post(app.urls.classroom_select, {"fk_unit":$scope.form.fk_unit}, function (data) {
				if ("0000" === data.code) {
					$scope.classroomList = data.data;
					$scope.findmajor();
				} else {
					alert(data.msg);
				}
			});
		}

		//查询专业
		$scope.findmajor = function() {
			httpService.post(app.urls.major_select, {"fk_unit":$scope.form.fk_unit}, function(data) {
				if("0000" === data.code){
					$scope.majorList = data.data;
					$scope.getName();
				}else{
					alert(data.msg);
				}
			});
		}

		httpService.post(app.urls.class_selectUnitGrade, {"id":params.id}, function(data) {
			if("0000" === data.code) {
				$scope.form= data.data[0];
				if($scope.form.major != null && $scope.form.major != "") {
					$('#major_name')[0].innerHTML = $scope.form.major;
				}
				$scope.findUnit();
			}else{
				alert(data.msg);
			}
		});
    	
    	$scope.getName = function() {
			$scope.form.gradename = $('#grade').find("option:selected").text();
			if($scope.form.fk_grade != undefined &&$scope.form.fk_grade !=null && $scope.form.fk_grade != ''){
				$scope.form.sort = $scope.form.sort.replace(/^(0+)|[^\d]+/g,'');
				$scope.form.show = true;
			}else{
				$scope.form.show = false;
			}
    	}
		$('input[name="classNo"]').bind('input propertychange',function(){
			$scope.form.sort = $scope.form.sort.toString().replace(".","");
			$scope.getName();
			$scope.$apply();
		});
		$('input[name="major"]').bind('input propertychange',function(){
			$scope.getName();
			$scope.$apply();
		});
    	
		$scope.saveForm = function() {
			if($scope.formCheck()) {

				if($scope.isClassMajorShow == "1"){
					$scope.form.major = $('#major_name')[0].innerHTML;
					$scope.form.name = $scope.form.gradename + $scope.form.major +$scope.form.sort+"班";
				}else{
					$scope.form.name = $scope.form.gradename+$scope.form.sort+"班";
				}

				httpService.post(app.urls.class_selectWhenUpdate,$scope.form,function(data){
					if("0000" === data.code){
						if(data.data.length == 0){
							httpService.post(app.urls.class_update,$scope.form,function(data){
								if("0000" === data.code){
									eventBusService.publish("content.page.reload", data);
									eventBusService.publish("modal.page.close", {});
								}else{
									alert(data.msg);
								}
							});

						}else{
							alert("该班级已存在");
						}
					}else{
						alert(data.msg);
					}
				});
			}
		}
	}}});
